package com.woniuxy.aspectj;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

/**
 * @Author 罗蔚
 * @Date 2022/3/28 16:21
 * @Description TODO
 */
public class MyAspectJ {
    public void myBefore(JoinPoint joinpoint){
        System.out.println("-----------------------");

        System.out.println("前置通知：日志打印开始……");
        System.out.println("对应目标类是："+joinpoint.getTarget());
        System.out.println("对应目标类的方法是："+joinpoint.getSignature().getName());
        System.out.println("-----------------------");
    }
    public void afterReturning(JoinPoint joinpoint){
        System.out.println("-----------------------");
        System.out.println("后置通知：事务提交开始……");
        System.out.println("对应目标类是："+joinpoint.getTarget());
        System.out.println("对应目标类的方法是："+joinpoint.getSignature().getName());
        System.out.println("-----------------------");
    }
    public void after(){
        System.out.println("----------------------------------------");
        System.out.println("最终通知：无论你的代码是否发生异常，都会执行。");
        System.out.println("----------------------------------------");
    }

    public void afterThrowing(Exception e){
        System.out.println("----------------------------------------");
        System.out.println("异常通知：报错类型是"+e.getMessage());
        System.out.println("----------------------------------------");
    }

    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("环绕通知：前置通知……");
        //目标方法的入参
        Object[] args = joinPoint.getArgs();
        for (Object arg : args) {
            System.out.println(arg);
        }
        args[0] = 9;
        args[1] = 10;
        //环绕通知中，目标方法的执行
        Object proceed = joinPoint.proceed(args);
        System.out.println(proceed);
        System.out.println("环绕通知：后置通知……");
        return (int)proceed+1;
    }
}
